//
//  WBWelcomeViewController.swift
//  weibo
//
//  Created by  郭航 on 16/6/26.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeViewController: UIViewController {
    
    private lazy var backImage: UIImageView = UIImageView(imageName: "ad_background")
    
    private lazy var iconImage: UIImageView = {
        let imageView = UIImageView()
        
        imageView.layer.cornerRadius = 42.5
        imageView.layer.borderColor = APPTintColor.CGColor
        imageView.layer.borderWidth = 3
        imageView.layer.masksToBounds = true
        
        imageView.sd_setImageWithURL(NSURL(string: WBUserInfor.shareWBUserInfor.userInforModel?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return imageView
    }()
    
    private lazy var label: UILabel = {
        let label = UILabel()
        
        if let name = WBUserInfor.shareWBUserInfor.userInforModel?.screen_name {
            label.text = "欢迎回来 \(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        label.textColor = UIColor.grayColor()
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(20)
        label.alpha = 0
        
        return label
    }()

    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(iconImage)
        
        iconImage.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.bottom.equalTo(self.view).offset(-175)
            make.centerX.equalTo(self.view)
        }
        
        view.addSubview(label)
        
        label.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconImage.snp_bottom).offset(16)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconImage.snp_remakeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.top.equalTo(self.view).offset(100)
            make.centerX.equalTo(self.view)
        }
        
//        iconImage.snp_updateConstraints { (make) in
//            make.bottom.equalTo(self.view).offset(-550)
//        }
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(0.25, animations: { 
                    self.label.alpha = 1
                    }, completion: { (_) in
                        NSNotificationCenter.defaultCenter().postNotificationName(NotChangeKeyWindows, object: "welcome")
                        //UIApplication.sharedApplication().keyWindow?.rootViewController = WBMainViewController()
                        
                })
        }
    }

}
